//------------------------------时间显示--上--------------------------------------------------------


function appendZero(obj)     
    {    
        if(obj<10) return "0" +""+ obj; //判断obj是否小于10，如果小于10，则在数字前面加上"0"补位    
        else return obj;     
    }      
function showTime(){

    var now = new Date();

    var year = now.getFullYear(); //得到年份

    var month = now.getMonth()+1;//得到月份

    var date = now.getDate();//得到日期

    var hour= appendZero(now.getHours());//得到小时数

    var minute= appendZero(now.getMinutes()) ;//得到分钟数

// document.getElementById("y").innerHTML=year;

// document.getElementById("mo").innerHTML=month;

// document.getElementById("s").innerHTML=date;

// document.getElementById("h").innerHTML=hour;

// document.getElementById("m").innerHTML=minute;

// document.getElementById("s").innerHTML=second;
document.getElementById("timeText").innerHTML = String(year) +"/"+ String(month) +"/"+ String(date) +" "+ String(hour) +":"+ String(minute);


setInterval("showTime()",1000);//定时器一直调用show()函数

}
showTime();

//------------------------------时间显示---下------------------------------------------------------


//------------------------------点击房间---上-------------------------------------------------------

var room_frame = document.getElementById("room-frame");
var customer_frame  =document.getElementById("customer-frame");

var checkinFrame = document.getElementById("checkin");
var checkoutFrame = document.getElementById("checkout");
var repairFrame = document.getElementById("repair");

var askFrame1 = document.getElementById('askForEmptyRoom');//询问框1
var askFrame2 = document.getElementById('askForOccupiedRoom');//询问框2

var bg = document.getElementById('bg');//遮盖层

var changeRoomFrame = document.getElementById("changeRoom");

var realTimeRoomNumber = "";

var p_title=document.getElementsByClassName("tips");


function clickRoom(obj){
    obj.className="clicked";
    if(obj.value == "available"){
        realTimeRoomNumber = obj.id;
        askForEmptyRoom();
    }else if(obj.value == "occupied"){
        realTimeRoomNumber = obj.id;
        askForOccupiedRoom();
    }else{
        realTimeRoomNumber = obj.id;
        askForFixedRoom();
        
    }
    
}

var table = document.getElementById("customerTable");//客户信息表

function displaycustomer(){
    room_frame.style.display="none";
    customer_frame.style.display="block";
}
function displayroom(){
    customer_frame.style.display="none";
    room_frame.style.display="grid";
}

function askForEmptyRoom(){ //显示询问框和遮盖层

    askFrame1.style.display="block";
    bg.style.display="block";
}

function askForOccupiedRoom(){

    askFrame2.style.display = "block";
    bg.style.display="block";
}

function checkin(){//填写入住信息

    askFrame1.style.display="none";
    checkinFrame.style.display = "block";

}

function checkinDone(){

    var name = document.getElementById("nameInput").value; //姓名
    var idNumber = document.getElementById("idNumberInput").value; //身份证号码
    var sex = ((idNumber[16]%2)==0 ? 0 : 1)==0?"女":"男"; //性别
    
    if(name==""||idNumber==""){
        alert("输入不能为空");
        return;
    }

    if(String(idNumber).length!=18){
        alert("请输入18位身份证号");
        return;
    }

    document.getElementById(realTimeRoomNumber+"name").innerHTML = name;
    document.getElementById(realTimeRoomNumber+"sex").innerHTML = sex;
    document.getElementById(realTimeRoomNumber+"idNumber").innerHTML = idNumber;

    document.getElementById("nameInput").value = ""; //姓名
    document.getElementById("idNumberInput").value = ""; //身份证号码

    document.getElementById(realTimeRoomNumber).value = "occupied";

    bg.style.display="none";
    checkinFrame.style.display = "none";
    document.getElementById(realTimeRoomNumber).className="";
    realTimeRoomNumber = "";

    frameReset();
    
}


function returnToMain(){
    askFrame1.style.display="none";
    bg.style.display="none";
    checkinFrame.style.display = "none";
}

function whetherCheckout(){
    askFrame2.style.display = "none";
    checkoutFrame.style.display = "block";
}

function whetherSetRoomFixed(){
    askFrame1.style.display = "none";
    askFrame2.style.display = "none";
    bg.style.display="none";
    var r=confirm("设置房间维修？");
        if (r==true){
            
            if(document.getElementById(realTimeRoomNumber).value == "occupied"){
                changeRoomFrame.style.display  = "block";
                bg.style.display = "block";
                //cancel();
            }else{
                document.getElementById(realTimeRoomNumber).value = "maintained";
                document.getElementById(realTimeRoomNumber).className="";
                realTimeRoomNumber = "";
                
                
            }

            
        }else{
            
            document.getElementById(realTimeRoomNumber).className="";
            realTimeRoomNumber = "";
            
        }
        
}


function changeRoom(){
    
    newRoomNumber = document.getElementById("newRoomInput").value;
    if(document.getElementById(newRoomNumber).value == "occupied"||document.getElementById(newRoomNumber).value == "maintained"){
        alert("房间不可用 请选择显示为绿色的房间");
         
        return;
    }else{
        document.getElementById(newRoomNumber+"name").innerHTML = document.getElementById(realTimeRoomNumber+"name").innerHTML;
        document.getElementById(newRoomNumber+"sex").innerHTML = document.getElementById(realTimeRoomNumber+"sex").innerHTML;
        document.getElementById(newRoomNumber+"idNumber").innerHTML = document.getElementById(realTimeRoomNumber+"idNumber").innerHTML;

        document.getElementById(realTimeRoomNumber+"name").innerHTML = "";
        document.getElementById(realTimeRoomNumber+"sex").innerHTML = "";
        document.getElementById(realTimeRoomNumber+"idNumber").innerHTML = "";

        document.getElementById(realTimeRoomNumber).value = "maintained";

        document.getElementById(realTimeRoomNumber).className="";
        realTimeRoomNumber = "";
        
        document.getElementById(newRoomNumber).value = "occupied";
        document.getElementById("newRoomInput").value = ""; 
        alert("换房成功");
        
    }
    cancel();
}

function checkout(){//点击了确定退房后的事情
    checkoutFrame.style.display = "none";
    bg.style.display="none";

    
    document.getElementById(realTimeRoomNumber+"name").innerHTML = "";
    document.getElementById(realTimeRoomNumber+"sex").innerHTML = "";
    document.getElementById(realTimeRoomNumber+"idNumber").innerHTML = "";
   
    document.getElementById(realTimeRoomNumber).value  = "available";
    document.getElementById(realTimeRoomNumber).className="";
    realTimeRoomNumber = "";
    frameReset();
    
}

function cancel(){

    frameReset();
   
    askFrame1.style.display="none";


    
    askFrame2.style.display = "none";

    bg.style.display="none";

    
    checkinFrame.style.display = "none";

    
    checkoutFrame.style.display = "none";

    
    changeRoomFrame.style.display = "none";
    

    document.getElementById(realTimeRoomNumber).className="";
    realTimeRoomNumber = "";
    
    document.getElementById("nameInput").value = ""; 
    document.getElementById("idNumberInput").value = ""; 
    document.getElementById("newRoomInput").value = "";

    
    
}

function frameReset(){
    askFrame1.style.left = 454.396+'px';
    askFrame1.style.top = 122.333+'px';
    askFrame2.style.left = 454.396+'px';
    askFrame2.style.top = 122.333+'px';
    checkinFrame.style.left = 454.396+'px';
    checkinFrame.style.top = 122.333+'px';
    checkoutFrame.style.left = 454.396+'px';
    checkoutFrame.style.top = 122.333+'px';
    changeRoomFrame.style.left = 454.396+'px';
    changeRoomFrame.style.top = 122.333+'px';

}


function askForFixedRoom(){
    var r=confirm("维修完毕？");
        if (r==true){
            document.getElementById(realTimeRoomNumber).value = "available";
            document.getElementById(realTimeRoomNumber).className="";
            
        }else{
            document.getElementById(realTimeRoomNumber).className="";
            realTimeRoomNumber = "";
            
        }
        
}


//var p_title=document.getElementsByClassName("tips");这句话放在最上面去了
for(var i in p_title){
    p_title[i].onmousedown=drag;
    p_title[i].onmouseup=loose;
}

function loose(){
    document.onmousemove=null;
                
}
function drag(e){
    e = e || window.event;
    var x=e.pageX || e.clientX +(document.body.scrollLeft || document.documentElement.scrollLeft);
    var y=e.pageY || e.clientY +(document.body.scrollTop || document.documentElement.scrollTop);
                
    var popup=this.parentNode;

    var boxX=popup.offsetLeft;
    var boxY=popup.offsetTop;

    var mouse_in_boxX=x-boxX;
    var mouse_in_boxY=y-boxY;

    document.onmousemove=function(e){
        var x=e.pageX || e.clientX +(document.body.scrollLeft || document.documentElement.scrollLeft);
        var y=e.pageY || e.clientY +(document.body.scrollTop || document.documentElement.scrollTop);

        popup.style.left=x-mouse_in_boxX+'px';
        popup.style.top=y-mouse_in_boxY+'px';
    }
}